<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/vue.js"></script>
  <script src="../js/vuex.js"></script>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <!-- 引入组件库 -->
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <script src="../js/axios.js"></script>
</head>

<body>
  <div id="app">
    {{msg}}
    <el-table :data="categories" style="width: 100%">
      <el-table-column prop="name" label="栏目名称" align="center"></el-table-column>
      <el-table-column prop="parentId" label="所属栏目"></el-table-column>
      <el-table-column prop="description" label="栏目描述" align="center"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button @click="deleteHandler(row.id)" type="text" size="small">删除</el-button>
          <el-button @click="editHandler(row)" type="text" size="small">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <script>
    let store = new Vuex.Store({
      state: {
        categories: []
      },
      mutations: {
        SET_CATEGORY(state, payload) {
          state.categories = payload
        }
      },
      actions: {
        // 声明异步请求
        async findAllCategories({ commit }) {
          // 配置基路径
          axios.defaults.baseURL = 'http://47.94.46.113:8888'
          let res = await axios.get('/index/category/findAll')
          // 提交突变，利用突变将res中的数据赋值给state中的categories
          commit('SET_CATEGORY', res.data.data)
        }
      }
    })

    let { mapState, mapActions } = Vuex

    let vm = new Vue({
      el: '#app',
      data: {
        msg: 'hello vue'
      },
      computed: {
        // 映射状态机中的静态数据
        ...mapState(['categories'])
      },
      methods: {
        // 映射状态机中的动作
        ...mapActions(['findAllCategories']),
        // 删除按钮点击事件
        deleteHandler(id) {
          console.log(id);
        },
        // 修改按钮点击事件
        editHandler(row) {
          console.log(row);
        }
      },
      created() {
        this.findAllCategories()
      },
      store
    })
  </script>
</body>

</html>